package com.example.backstage.controller;

import com.example.backstage.service.IOrderService;
import com.example.backstage.service.UserService;
import com.example.backstage.vo.OrderVo;
import com.example.gok_mall.entity.User;
import com.example.gok_mall.reponseData.ResultData;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@ResponseBody
@RequestMapping("/manage/order")
public class OrderManageController {

    @Autowired
    private UserService userService;

    @Autowired
    private IOrderService iOrderService;

    //管理员查看订单列表接口
    @GetMapping("/order_list.do")
    public ResultData<PageInfo> orderList(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        //验证用户是否登录
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        if (userService.checkAdminRole(user).isSuccess()) {
            //是管理员，增加我们处理分类的逻辑
            return iOrderService.manageList(pageNum, pageSize);
        } else {
            return ResultData.fail("当前登录者不是管理员，无权限操作");
        }
    }

    //管理员查看订单详情接口
    @GetMapping("/order_detail.do")
    public ResultData orderDetail(HttpSession session, Long orderNo){
        //验证用户是否登录
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        if (userService.checkAdminRole(user).isSuccess()) {
            //是管理员，增加我们处理分类的逻辑
            return iOrderService.manageDetail(orderNo);
        }else {
            return ResultData.fail("当前登录者不是管理员，无权限操作");
        }
    }

    //管理员根据订单号搜索
    @GetMapping("/order_search.do")
    public ResultData<PageInfo> orderSearch(HttpSession session,Long orderNo,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        //验证用户是否登录
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        if (userService.checkAdminRole(user).isSuccess()) {
            //是管理员，增加我们处理分类的逻辑
            return iOrderService.manageSearch(orderNo, pageNum, pageSize);
        }else {
            return ResultData.fail("当前登录者不是管理员，无权限操作");
        }
    }

    //管理员根据订单号发货接口
    @PostMapping("/send_goods.do")
    public ResultData<String> sendGoods(HttpSession session,Long orderNo){
        //验证用户是否登录
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        if (userService.checkAdminRole(user).isSuccess()) {
            //是管理员，增加我们处理分类的逻辑
            return iOrderService.manageSendGoods(orderNo);
        }else {
            return ResultData.fail("当前登录者不是管理员，无权限操作");
        }
    }
}



